//
//  UIAlertController+JXE.swift
//  JXUtilsDemo
//
//  Created by ccks on 2018/11/28.
//  Copyright © 2018 Zhu Jinxin. All rights reserved.
//

import Foundation
import UIKit

extension UIAlertController {
    
    static func alertWith(title:String?,message:String?,messageAligment:NSTextAlignment = .center ,style:UIAlertController.Style, cancelTitle:String? , OkTitle:String?,
                         CancelActionHandle:((UIAlertAction)->Void)? ,OkActionHandle:((UIAlertAction)->Void)? ) ->(UIAlertController) {
        
        
        let alertController = UIAlertController(title: title ,
                                                message: message, preferredStyle:style)
        
        
        if cancelTitle != nil {
            let cancelAction = UIAlertAction(title: cancelTitle , style: .cancel, handler: CancelActionHandle)
            alertController.addAction(cancelAction)
        }
        
        if OkTitle != nil{
            let okAction = UIAlertAction(title: OkTitle , style: .default, handler: OkActionHandle)
            alertController.addAction(okAction)
        }
        
        alertController.setMessageAligment(aligment: messageAligment)
        
        return alertController
    }
    
    
    func setMessageAligment(aligment:NSTextAlignment){
        let subViews = view.subviews
        for view in subViews{
            if view.isMember(of: UILabel.self) && (view as? UILabel)?.text != title {
                (view as? UILabel)?.textAlignment = aligment
                break
            }
        }
        
    }
    
    
}
